﻿using Lifang.Common;
using Lifang.Entites;
using Lifang.Enum;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace Lifang.Dao
{
    public class BaseDao<T> where T : class, IBaseDbEntity<T>, new()
    {
        public static LoginUserModel OpUser = new LoginUserModel()
        {
            UserNo = "00000",
            UserName = "游客"
        };

        public BaseDao()
        {

        }

        public BaseDao(LoginUserModel loginUserModel)
        {
            if (loginUserModel != null)
            {
                OpUser = loginUserModel;
            }
        }


        #region 根据主键查实体
        /// <summary>
        /// 根据用户主键查用户实体
        /// </summary>
        /// <param name="id">用户表主键 </param>
        /// <returns>实体(非null)</returns>
        public T GetModById(int id)
        {
            return new DapperHelper().SelectModel<T>(id, new SqlNote("王孟贵", "根据用户主键查用户实体")) ?? new T();
        }
        #endregion


        #region  获取表所有数据
        public List<T> QueryList()
        {
            var list = new DapperHelper().SelectList<T>(new NameValueCollection(), new SqlNote("", ""));
            return list;
        }
        #endregion

        #region  获取表所有数据
        public List<T> QueryValidList()
        {
            var nvc = new NameValueCollection();
            var t = new T();

            if (t.GetFieldNames().ContainsKey("DataFlag"))
            {
                nvc.Add("DataFlag", DataFlagEnum.Valid.ToString("d"));
            }
            if (t.GetFieldNames().ContainsKey("IsDel"))
            {
                nvc.Add("IsDel", IsDelEnum.No.ToString("d"));
            }
            var list = new DapperHelper().SelectList<T>(new NameValueCollection(), new SqlNote("", ""));
            return list;
        }
        #endregion


    }
}
